package com.atguigu.gmall.product.controller;

import com.sun.org.apache.xpath.internal.SourceTree;
import org.aspectj.weaver.ast.Var;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author Sanmier
 * @create 2020-12-08 19:53
 */
//测试异步编排
public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // a线程----“hello”
        // b线程----“world”  1   3
        // c线程----“girl”   3   1

        //FutureA
        CompletableFuture<String> FutureA = CompletableFuture.supplyAsync(new Supplier<String>() {
            @Override
            public String get() {
                return "hello";
            }
        });
        //FutureB
        CompletableFuture<Void> FutureB = FutureA.thenAcceptAsync(new Consumer<String>() {
            @Override
            public void accept(String s) {
                delaySec(1);
                printCurrTime(s + "\t world");
            }
        });
        //FutureC
        CompletableFuture<Void> FutureC = FutureA.thenAcceptAsync(new Consumer<String>() {
            @Override
            public void accept(String s) {
                delaySec(3);
                printCurrTime(s + "\t girl");
            }
        });

        //调用方法
        System.out.println(FutureB.get());
        System.out.println(FutureC.get());

        //没有返回值
        //CompletableFuture.runAsync(()->{});
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("来啦?");
//            }
//        });
//
//        completableFuture.get();

        //有返回值
//        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(new Supplier<Integer>() {
//            @Override
//            public Integer get() {
//                System.out.println("来了！");
//                //int i =  1/0;
//                return 1024;
//            }
//        }).thenApply(new Function<Integer, Integer>() {
//            //获取上一个方法的返回值，同时进行自己的业务处理  并返回自己的结果
//            @Override
//            public Integer apply(Integer integer) {
//                System.out.println("thenApply:\t"+integer);
//                return integer*2;
//            }
//        }).whenComplete(new BiConsumer<Integer, Throwable>() {
//            @Override
//            public void accept(Integer integer, Throwable throwable) {
//                System.out.println("integer:"+integer);
//                System.out.println("throwable:"+throwable);
//            }
//        }).exceptionally(new Function<Throwable, Integer>() {
//            @Override
//            public Integer apply(Throwable throwable) {
//                System.out.println("exceptionally----throwable:"+throwable);
//                //异常处理
//                return 404;
//            }
//        });
//
//        System.out.println(integerCompletableFuture.get());
    }

    private static void printCurrTime(String s) {
        System.out.println(s);
    }

    private static void delaySec(int i) {
        try {
            Thread.sleep(i*1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
